package qingke.blog.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import qingke.blog.pojo.Article;
import qingke.blog.pojo.User;

public class BlogDao {

	private final String driver = "com.mysql.jdbc.Driver";
	private final String url = "jdbc:mysql://localhost:3306/web?useSSL=false";
	private final String u = "root";
	private final String p = "6775";

	private Connection conn = null;
	private String sql;
	private PreparedStatement ps = null;
	private ResultSet rs = null;

	public BlogDao() {
	}

	{
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, u, p);
			System.out.println("Connect to database successfully!");
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}

	public boolean isUserExists(String username, String password) {
		sql = "select 'Y' from user where username=? and password=?";

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean isUsernameExists(String username) {
		sql = "select * from user where username=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean register(String username, String password, String name) {
		if (isUsernameExists(username)) {
			return false;
		}
		sql = "insert into user(username,password,name) values(?,?,?)";

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			ps.setString(3, name);
			int isTrue = ps.executeUpdate();
			if (isTrue > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
				ps.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return false;
	}

	public User login(String username) {
		sql = "select * from user where username=?";

		User user = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setName(rs.getString("name"));
			}
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	public void writer(String articleName, String contentPath, long date,int userId) {
		sql = "insert into article(article_name,content_path,newstime,user_id) values(?,?,?,?)";
		System.out.println(articleName);
		System.out.println(contentPath);
		System.out.println(date);
		System.out.println(userId);
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, articleName);
			ps.setString(2, contentPath);
			ps.setTimestamp(3, new java.sql.Timestamp(date));
			ps.setInt(4, userId);
			int count=ps.executeUpdate();
			if(count>0){
				System.out.println("success!");
				return;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("failed");
	}
	
	public List<Article> getAllArticle(){
		List<Article> list = new ArrayList<Article>();
		sql="select * from article ";
		
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs!=null){
				while(rs.next()){
					Article at =new Article();
					at.setId(rs.getInt("id"));
					at.setArticleName(rs.getString("article_name"));
					at.setContentPath(rs.getString("content_path"));
					at.setDate(rs.getDate("newstime"));
					at.setUserId(rs.getInt("user_id"));
					list.add(at);
				}
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}
